﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DataAccess;
namespace tracnghiemonline2.Ajax.ASP
{
    public partial class Submit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string result = Request.QueryString["result"];
            int id = int.Parse(Request.QueryString["id"]);
            int user_id = 0;
            int minute = int.Parse(Request.QueryString["minute"]);
            if (Session["user_id"] != null)
                user_id = int.Parse(Session["user_id"].ToString());
            if (result != "")
            {
                int total = 0;
                for (int i = 0; i < result.Split(',').Length - 1; i++)
                {
                    int temp1 = int.Parse(result.Split(',')[i].Split('-')[0]);
                    var get_test2 = new TblTestDetailController().FetchAll().Where(x =>x.TestDetailId==temp1).Single();
                    int temp2 = int.Parse(result.Split(',')[i].Split('-')[1]);
                    if (temp2 == get_test2.TestDetailConfirm)
                    {
                        total++;
                    }
                }

                new TblResultController().Insert(id, user_id, total,DateTime.Now,minute, result);  
            }
            else
                new TblResultController().Insert(id, user_id, 0,DateTime.Now,minute, result);

            var get_test = new TblTestController().FetchAll().Where(x => x.TestId == id).Single();

            var get_result = new TblResultController().FetchAll().Where(x=>x.TestId==id).Last();

            if (get_test.TestExam == true)
                Response.Redirect("~/trang-chu");
            else
            {
                Response.Redirect("~/ajax/asp/rank_total_score.aspx?correct=" + get_result.ResultCorrect + "&id=" + get_test.TestId+"&user_id="+user_id+"&result="+get_result.ResultId+"&test_id="+id);
            }
        }
    }
}